package com.gaojun.mybatisspring;

import com.gaojun.mapper.OrderMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author ：gaojun
 * @date ：Created in 2022/9/26 21:45
 * @description：Mapper的factoryBean
 * @modified By：
 * @version: 1.0$
 */
public class GaoJunFactoryBean implements FactoryBean {
	private Class mapperInterface;
	private SqlSession sqlSession;

	public GaoJunFactoryBean(Class mapperInterface) {
		this.mapperInterface=mapperInterface;
	}

	@Autowired
	public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
		sqlSessionFactory.getConfiguration().addMapper(this.mapperInterface);
		this.sqlSession=sqlSessionFactory.openSession();
	}

	@Override
	public Object getObject() throws Exception {
		return sqlSession.getMapper(this.mapperInterface);
	}

	@Override
	public Class<?> getObjectType() {
		return this.mapperInterface;
	}
}
